/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import DAO.ProductDAO;
import DAO.TestDataDAO;
import GUI.Components.IPaginatorEvent;
import POJO.Product;
import POJO.Testdata;
import Util.GuiUtil;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import org.apache.log4j.Logger;

/**
 *
 * @author Nguyen Hong Phu (1142066) - 11HCB (HCMUS)
 */
public class FrmDemo extends JInternalFrame {
    private List<Testdata> listData;
    private long totalRecords;
    private final int perRecorsOnPage = 33;
    private int currentRecord = 0;
    private Logger log =  Logger.getLogger(this.getClass().getName());
    
    private final String choosenTextCellCboTable = "Chọn";
  
    
    private JComboBox jComboBox;
    
    /**
     * Creates new form FrmDemo
     */
    public FrmDemo() {
        super();
        initComponents();
        
        // Tính tổng số records
         try {
            totalRecords = TestDataDAO.getTotalList();
        } catch (Exception e) {
          log.error(e.fillInStackTrace());
          return;
        }
        
        // Load lần đầu tiên
        paginator1.init(totalRecords, perRecorsOnPage);
        this.updateTable();
       
        paginator1.addEventPerformClick(new IPaginatorEvent() {
            @Override
            public void performEventFirst() {
                updateTable();
            }

            @Override
            public void performEventPrev() {
                updateTable();
            }

            @Override
            public void performEventNext() {
                updateTable();
                 
            }

            @Override
            public void performEventLast() {
                updateTable();
            }
        });
   }

    private void updateTable(){
        this.currentRecord = paginator1.getCurrentRecord();
        try {
            listData = TestDataDAO.getListTestData(this.currentRecord, perRecorsOnPage);
        } catch (Exception e) {
           JOptionPane.showMessageDialog(rootPane, e.getMessage());
           return;
        }

        DefaultTableModel dtm = (DefaultTableModel) jTable1.getModel();
        // xóa dữ liệu của trang cũ
        dtm.getDataVector().removeAllElements();
        // Tạo Table model mới
        for(Testdata t : listData){
            Object[] dataRow = {
                t.getKey(), choosenTextCellCboTable
            };
            dtm.addRow(dataRow);
        }
        jTable1.setModel(dtm);
        List<String> list = new ArrayList<>();
        for (int i = 0; i < 30; i++) {
            list.add("Demo "+i);
        }
        List<POJO.Product> listP = null;
        try {
             listP = ProductDAO.getListProducts(1);
        } catch (Exception ex) {
            java.util.logging.Logger.getLogger(FrmDemo.class.getName()).log(Level.SEVERE, null, ex);
        }
       
           
        jComboBox.addItemListener(new ItemListener() {

            @Override
            public void itemStateChanged(ItemEvent e) {
               if(e.getStateChange() == ItemEvent.SELECTED){
                   Product p  = (Product) e.getItem();
                   JOptionPane.showMessageDialog(rootPane, p.getName());
                  
               }
            }
           
        });
        // Them cbo vao table 
        GuiUtil.addComboboxToTable(jTable1, 1, jComboBox, listP, "getName", choosenTextCellCboTable);
        
        
    }
  
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        paginator1 = new GUI.Components.Paginator();
        jButton1 = new javax.swing.JButton();
        jComboBox1 = new javax.swing.JComboBox();

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Title 1", "Title 2"
            }
        ));
        jScrollPane1.setViewportView(jTable1);
        jTable1.getColumnModel().getColumn(1).setResizable(false);

        jButton1.setText("jButton1");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jComboBox1.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jComboBox1ItemStateChanged(evt);
            }
        });
        jComboBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 527, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 50, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGap(62, 62, 62)
                .addComponent(paginator1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jButton1)
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(50, 50, 50)
                .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 345, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(paginator1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1))
                .addContainerGap(19, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        DefaultTableModel tb = (DefaultTableModel) jTable1.getModel();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox1ActionPerformed
        System.out.println("Change ");
    }//GEN-LAST:event_jComboBox1ActionPerformed

    private void jComboBox1ItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jComboBox1ItemStateChanged
       if(evt.getStateChange() == ItemEvent.SELECTED )
        JOptionPane.showMessageDialog(rootPane, "Change");
    }//GEN-LAST:event_jComboBox1ItemStateChanged

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private GUI.Components.Paginator paginator1;
    // End of variables declaration//GEN-END:variables

  
}
